package com.emep.changzhi.analyse.http.information;


import com.emep.changzhi.analyse.config.RabbitConfig;
import com.emep.changzhi.analyse.entity.information.SysPromptInfo;
import com.emep.changzhi.analyse.entity.information.SysWarningRule;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.information.SysPromptInfoService;
import com.emep.changzhi.analyse.service.information.SysWaringRuleService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import com.emep.changzhi.analyse.utils.Regulation;
import com.rabbitmq.client.Channel;
import io.swagger.annotations.*;
import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.MapContext;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**

 * @Description: 报警规则信息控制器
 */
@RestController
@RequestMapping("syswarningrule")
@Api(value="报警规则信息controller",tags={"报警规则信息"})
public class SysWarningRuleController {


    @Autowired
    private SysWaringRuleService service;


    /**
     * 新增 报警规则信息
     * @param payload
     * @return
     */
    @ApiOperation(value="新增 报警规则信息", notes="新增 报警规则信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warningRule", value = "报警规则", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "warningRuleName", value = "报警规则名", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "receiver", value = "接收人id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "template", value = "模板", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "sendType", value = "发送规则id", required = true, dataType = "String",paramType="query")
    })
    @PostMapping(value = "add")
    public ResultBean<SysWarningRule> add(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "warningRule");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.boolAdd(service.addWaringRule(payload));
        }
        return  result;
    }


    /**
     * 查询所有 报警规则信息
     * @return
     */
    @ApiOperation(value="获取报警规则信息列表", notes="获取报警规则信息列表")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @GetMapping(value = "list")
    public ResultBean<List<SysWarningRule>> list(){
        return new ResultBean<>(service.getList());
    }


    /**
     * 根据id 删除 报警规则信息
     * @param payload
     */
    @ApiOperation(value="删除 报警规则信息", notes="删除 报警规则信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParam(name = "id", value = " 报警规则信息id", required = true, dataType = "String",paramType = "query")
    @DeleteMapping(value = "/delete")
    public ResultBean<Boolean> delete(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.bool(service.delete(payload.get("id").toString()));
        }
        return result;
    }



    /**
     * 实时报警测试
     * @param payload
     * @return
     */

    @ApiOperation(value="测试报警规则信息", notes="新增 报警规则信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warningRuleName", value = "报警名称", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "parameter", value = "参数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "source", value = "站点类型（FANOUT_QUEUE_CONTROLSITE，FANOUT_QUEUE_MICROSTATION，FANOUT_QUEUE_ENTERPRICE）", required = true, dataType = "String",paramType="query")
    })
    @PostMapping(value = "test")
    public ResultBean<String> test(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "warningRuleName", "parameter","source");

        String warningRuleName=payload.get("warningRuleName").toString();
        String parameter=payload.get("parameter").toString();
        String source =payload.get("source").toString();
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.ok(service.getRule(warningRuleName ,parameter,source));
        }
        return result;
    }

    /**
     *
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {RabbitConfig.controlSite})
    public void messagea(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        System.out.println(RabbitConfig.controlSite+new String(message.getBody()));
    }

    /**
     *
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {RabbitConfig.enterprise})
    public void messageb(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        System.out.println(RabbitConfig.enterprise+new String(message.getBody()));
    }

    /**
     *
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {RabbitConfig.microStation})
    public void messagec(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        System.out.println(RabbitConfig.microStation+new String(message.getBody()));
    }


}
